package com.atguigu.singleton.type7;

/**
 * @ClassName SingleTon07
 * @Description 单例模式-静态内部类
 * @Author HongJian-Yang
 * @Date 2020/6/16 23:13
 * @Version 1.0
 */
public class SingleTon07 {
    public static void main(String[] args) {
        SingleTon instance = SingleTon.getInstance();
        System.out.println(instance == SingleTon.getInstance());
    }
}

class SingleTon {
    private SingleTon() {
    }

    public static SingleTon getInstance() {
        return SingleTonInstance.INSTANCE;
    }

    // 使用类加载机制创建单例对象,只有在调用getInstance方法的时候才会装载这个类
    public static class SingleTonInstance {
        private static final SingleTon INSTANCE = new SingleTon();
    }
}
